/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.warehouse.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.modules.warehouse.entity.BIWarehouseKpiData;
import com.thinkgem.jeesite.modules.warehouse.entity.WarehouseKpiMonitoring;
import com.thinkgem.jeesite.modules.warehouse.service.WarehouseKpiMonitoringService;

/**
 * 仓库KPI监控Controller
 * 
 * @author hui.li
 * @version 2016-12-23
 */
@Controller
@RequestMapping(value = "${adminPath}/warehouse/warehouseKpiMonitoring")
public class WarehouseKpiMonitoringController extends BaseController {

	@Autowired
	private WarehouseKpiMonitoringService warehouseKpiMonitoringService;

	@ModelAttribute
	public WarehouseKpiMonitoring get(@RequestParam(required = false) String id) {
		WarehouseKpiMonitoring entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = warehouseKpiMonitoringService.get(id);
		}
		if (entity == null) {
			entity = new WarehouseKpiMonitoring();
		}
		return entity;
	}

	/*
	 * 仓库BI报表工时
	 */
	@RequiresPermissions("warehouse:warehouseKpiMonitoring:view")
	@RequestMapping(value = { "list", "" })
	public String list(WarehouseKpiMonitoring warehouseKpiMonitoring,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<WarehouseKpiMonitoring> page = warehouseKpiMonitoringService
				.findPage(new Page<WarehouseKpiMonitoring>(request, response),
						warehouseKpiMonitoring);
		model.addAttribute("page", page);
		model.addAttribute("warehouseKpiMonitoring", warehouseKpiMonitoring);
		return "modules/warehouse/warehouseKpiMonitoringList";
	}

	@RequiresPermissions("warehouse:warehouseKpiMonitoring:view")
	@RequestMapping(value = "form")
	public String form(WarehouseKpiMonitoring warehouseKpiMonitoring,
			Model model) {
		model.addAttribute("warehouseKpiMonitoring", warehouseKpiMonitoring);
		return "modules/warehouse/warehouseKpiMonitoringForm";
	}

	@RequiresPermissions("warehouse:warehouseKpiMonitoring:edit")
	@RequestMapping(value = "save")
	public String save(WarehouseKpiMonitoring warehouseKpiMonitoring,
			Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, warehouseKpiMonitoring)) {
			return form(warehouseKpiMonitoring, model);
		}
		warehouseKpiMonitoringService.save(warehouseKpiMonitoring);
		addMessage(redirectAttributes, "保存仓库KPI监控成功");
		return "redirect:" + Global.getAdminPath()
				+ "/warehouse/warehouseKpiMonitoring/?repage";
	}

	@RequiresPermissions("warehouse:warehouseKpiMonitoring:edit")
	@RequestMapping(value = "delete")
	public String delete(WarehouseKpiMonitoring warehouseKpiMonitoring,
			RedirectAttributes redirectAttributes) {
		warehouseKpiMonitoringService.delete(warehouseKpiMonitoring);
		addMessage(redirectAttributes, "删除仓库KPI监控成功");
		return "redirect:" + Global.getAdminPath()
				+ "/warehouse/warehouseKpiMonitoring/?repage";
	}

	/**
	 * 导入用户数据
	 * 
	 * @param file
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("warehouse:warehouseKpiMonitoring:edit")
	@RequestMapping(value = "import", method = RequestMethod.POST)
	public String importFile(MultipartFile file,
			RedirectAttributes redirectAttributes) {
		if (Global.isDemoMode()) {
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath
					+ "/warehouse/warehouseKpiMonitoring/list?repage";
		}
		try {
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 0, 0);
			List<WarehouseKpiMonitoring> list = ei
					.getDataList(WarehouseKpiMonitoring.class);
			for (WarehouseKpiMonitoring wa : list) {
				try {
					BeanValidators.validateWithException(validator, wa);
					warehouseKpiMonitoringService.save(wa);
				} catch (ConstraintViolationException ex) {
					failureMsg.append(" 导入失败：");

				} catch (Exception ex) {
					failureMsg.append("导入失败：" + ex.getMessage());
				}
			}
			addMessage(redirectAttributes, "已成功导入 ");
		} catch (Exception e) {
			addMessage(redirectAttributes,
					"导入仓库KPI监控仪表盘失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath
				+ "/warehouse/warehouseKpiMonitoring/list?repage";
	}

	/**
	 * 下载导入用户数据模板
	 * 
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("sys:user:view")
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			String fileName = "仓库KPI监控仪表盘.xlsx";
			List<WarehouseKpiMonitoring> list = Lists.newArrayList();
			list.add(warehouseKpiMonitoringService.get("22"));
			new ExportExcel("仓库KPI监控仪表盘", WarehouseKpiMonitoring.class, 2)
					.setDataList(list).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath
				+ "/warehouse/warehouseKpiMonitoring/list?repage";
	}

	/**
	 * 德国考勤
	 * 
	 * @param biWarehouseKpiData
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("warehouse:biwrehouseKpi:view")
	@RequestMapping(value = { "biwrehouseKpiList", "" })
	public String findAllListBIWarehouseKpi(
			BIWarehouseKpiData biWarehouseKpiData, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<BIWarehouseKpiData> page = warehouseKpiMonitoringService
				.findAllListBIWarehouseKpi(new Page<BIWarehouseKpiData>(
						request, response), biWarehouseKpiData);
		model.addAttribute("page", page);
		model.addAttribute("biWarehouseKpiData", biWarehouseKpiData);
		return "modules/warehouse/biwrehouseKpiList";
	}

	/**
	 * 中国，英国，美国统一模板有效工时BI报表 工时
	 * 
	 * @param biWarehouseKpiData
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("warehouse:templateBiwrehouseKpi:view")
	@RequestMapping(value = { "templateBIwrehouseKpiList", "" })
	public String findAllListTemplateBIWarehouseKpi(
			@RequestParam(required = true) String warehouse,
			BIWarehouseKpiData biWarehouseKpiData, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// 设置仓库
		if (null != warehouse && warehouse.length() > 0) {
			biWarehouseKpiData.setWarehouse(warehouse);
			model.addAttribute("warehouse", warehouse);
		}

		Page<BIWarehouseKpiData> page = warehouseKpiMonitoringService
				.findAllListTemplateBIWarehouseKpi(
						new Page<BIWarehouseKpiData>(request, response),
						biWarehouseKpiData);
		model.addAttribute("page", page);
		model.addAttribute("biWarehouseKpiData", biWarehouseKpiData);
		return "modules/warehouse/templateWrehouseKpiList";
	}

	/**
	 * 导出德国考勤数据
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("warehouse:biwrehouseKpi:view")
	@RequestMapping(value = "export", method = RequestMethod.POST)
	public String exportFile(HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes,
			BIWarehouseKpiData biWarehouseKpiData) {
		try {
			String fileName = "仓库KPI监控BI报表"
					+ DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Page<BIWarehouseKpiData> page = warehouseKpiMonitoringService
					.findAllListBIWarehouseKpi(new Page<BIWarehouseKpiData>(
							request, response, -1), biWarehouseKpiData);
			new ExportExcel("仓库KPI监控BI报表", BIWarehouseKpiData.class)
					.setDataList(page.getList()).write(response, fileName)
					.dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出考勤失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath()
				+ "/warehouse/warehouseKpiMonitoring/?repage";
	}
	
	/**
	 * 导出考勤数据中 国，英国，美国统一模板有效工时BI报表 工时
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("warehouse:templateBIwrehouseKpi:view")
	@RequestMapping(value = "exportCN", method = RequestMethod.POST)
	public String exportFile(@RequestParam(required = true) String warehouse,HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes,
			BIWarehouseKpiData biWarehouseKpiData) {
		try {
			// 设置仓库
			if (null != warehouse && warehouse.length() > 0) {
				biWarehouseKpiData.setWarehouse(warehouse);
			}
			String fileName = "仓库KPI监控BI报表"
					+ DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Page<BIWarehouseKpiData> page = warehouseKpiMonitoringService
					.findAllListTemplateBIWarehouseKpi(new Page<BIWarehouseKpiData>(
							request, response, -1), biWarehouseKpiData);
			new ExportExcel("仓库KPI监控BI报表", BIWarehouseKpiData.class)
					.setDataList(page.getList()).write(response, fileName)
					.dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出考勤失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath()
				+ "/warehouse/warehouseKpiMonitoring/?repage";
	}

}